;
; Author: Javier Quevedo Fernandez
; Reference: www.8052.com
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program; if not, write to the Free Software
; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

;; -------------------------------------------------------------
;; -------------------------------------------------------------
;;						RUTINAS 
;; -------------------------------------------------------------
;; -------------------------------------------------------------

ORG	0600H

div16_16:	
MOV B,#00h ;Clear B since B will count the number of left-shifted bits
MOV	R4,#0
MOV	R5,#0
MOV	R6,#0
MOV	R7,#0
div1:
  INC B     ;Increment counter for each left shift
  MOV A,R2  ;Move the current divisor low byte into the accumulator
  RLC A     ;Shift low-byte left, rotate through carry to apply highest bit to high-byte
  MOV R2,A  ;Save the updated divisor low-byte
  MOV A,R3  ;Move the current divisor high byte into the accumulator
  RLC A     ;Shift high-byte left high, rotating in carry from low-byte
  MOV R3,A  ;Save the updated divisor high-byte
  JNC div1  ;Repeat until carry flag is set from high-byte

div2:        ;Shift right the divisor
  MOV A,R3   ;Move high-byte of divisor into accumulator
  RRC A      ;Rotate high-byte of divisor right and into carry
  MOV R3,A   ;Save updated value of high-byte of divisor
  MOV A,R2   ;Move low-byte of divisor into accumulator
  RRC A      ;Rotate low-byte of divisor right, with carry from high-byte
  MOV R2,A   ;Save updated value of low-byte of divisor
  CLR C      ;Clear carry, we don't need it anymore
  MOV 07h,R1 ;Make a safe copy of the dividend high-byte
  MOV 06h,R0 ;Make a safe copy of the dividend low-byte
  MOV A,R0   ;Move low-byte of dividend into accumulator
  SUBB A,R2  ;Dividend - shifted divisor = result bit (no factor, only 0 or 1)
  MOV R0,A   ;Save updated dividend 
  MOV A,R1   ;Move high-byte of dividend into accumulator
  SUBB A,R3  ;Subtract high-byte of divisor (all together 16-bit substraction)
  MOV R1,A   ;Save updated high-byte back in high-byte of divisor
  JNC div3   ;If carry flag is NOT set, result is 1
  MOV R1,07h ;Otherwise result is 0, save copy of divisor to undo subtraction
  MOV R0,06h
div3:
  CPL C      ;Invert carry, so it can be directly copied into result
  MOV A,R4 
  RLC A      ;Shift carry flag into temporary result
  MOV R4,A   
  MOV A,R5
  RLC A
  MOV R5,A		
  DJNZ B,div2 ;Now count backwards and repeat until "B" is zero
  MOV R3,05h  ;Move result to R3/R2
  MOV R2,04h  ;Move result to R3/R2
  RET



Hex2BCD:
  
 
        MOV R3,#00D
        MOV R4,#00D
        MOV R5,#00D
        MOV R6,#00D
        MOV R7,#00D
 
MOV B,#10D
        MOV A,R2
        DIV AB
        MOV   R3,B              ;   
        MOV   B,#10             ; R7,R6,R5,R4,R3
        DIV   AB
        MOV   R4,B
        MOV   R5,A
        CJNE R1,#0H,HIGH_BYTE   ; CHECK FOR HIGH BYTE
        SJMP ENDD
 
HIGH_BYTE:
MOV   A,#6
        ADD   A,R3
MOV   B,#10
DIV   AB
        MOV   R3,B
ADD   A,#5
        ADD   A,R4
MOV   B,#10
DIV   AB
        MOV   R4,B
ADD   A,#2
        ADD   A,R5
MOV   B,#10
DIV   AB
        MOV   R5,B
        CJNE R6,#00D,ADD_IT
        SJMP CONTINUE
ADD_IT:
        ADD A,R6
CONTINUE:
        MOV R6,A
        DJNZ R1,HIGH_BYTE
        MOV B, #10D
        MOV A,R6
        DIV AB
        MOV R6,B
        MOV R7,A
ENDD:   RET